<template>
  <div class="user-sex">
    <van-picker
      title="性别"
      show-toolbar
      :columns="columns"
      @confirm="onConfirm"
      @cancel="onCancel"
      @change="onChange"
    />
  </div>
</template>

<script>
import { editUserProfile } from '@/api/user'
export default {
  name: 'UserSex',
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女'],
      localSex: this.value
    }
  },
  methods: {
    async onConfirm (value, index) {
      try {
        let gender = 0
        if (value === '女') {
          gender = 1
        }
        await editUserProfile({
          gender: gender
        })
        this.localSex = gender
        // 更新视图 关闭弹层 提示成功
        this.$emit('input', this.localSex)
        this.$emit('closePop')
        this.$toast.success('修改成功')
      } catch (err) {
        this.$toast('修改失败，稍后重试')
      }
    },
    onChange (picker, value, index) {
    },
    onCancel () {
      this.$emit('closePop')
    }
  }
}
</script>

<style scoped lang="less">
</style>
